<?php
namespace BLL\DataModel;
use Exception;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DataType
 *
 * @author David Wu
 */
class DataType {
    
    const Int = "Int";
    const Bool = "Bool";
    const String = "String";
    const Currency = "Currency";
    const Date = "Date";
    const DateTime = "DateTime";
    const Time = "Time";
    const Text = "Text";
    const BigInt = "BigInt";


    public static function createByName($string){

        if (strcasecmp($string,"Int")==0 || strcasecmp($string,"Integer")==0) {
            return DataType::Int;
        }else
        if (strcasecmp($string,"String") ==0  || strcasecmp($string,"varchar")==0) {
            return DataType::String;
        }else
        if (strcasecmp($string,"Bool")==0) {
            return DataType::Bool;
        }else
        if (strcasecmp($string,"Currency")==0) {
            return DataType::Currency;
        }else
        if (strcasecmp($string,"DateTime")==0) {
            return DataType::DateTime;
        }else
        if (strcasecmp($string,"Time")==0) {
            return DataType::Time;
        }else
        if (strcasecmp($string,"Date")==0) {
            return DataType::Date;
        }
        if (strcasecmp($string,"Text")==0) {
            return DataType::Text;
        }
        if (strcasecmp($string,"BigInt")==0) {
            return DataType::BigInt;
        }
        throw new Exception("Unexpected DataType : ".$string);
    }
     /**
     * @assert (DataType::Int,"1") === 1
     * @assert (DataType::Int,"a") === $data["db"]
     */
    public static function getValueFromString($type, $val, $fmt=null){

        if ($type === DataType::Int) {
            if(is_string($val)){
                $val = str_replace("$", "", $val);
                $val = str_replace(",", "", $val);
            }
            if(is_numeric($val))
                return intval($val);
            else
                return 0;
        }
        if ($type === DataType::String) {
            return trim($val);
        }
        if ($type === DataType::Bool) {
            if (strcasecmp($val,"True") ===0  
                    || strcasecmp($val,"yes") ===0 
                    || strcasecmp($val,"T") ===0 
                    || $val === "1") {
                return true;
            } else {
                return false;
            }
        }
        if ($type === DataType::Currency) {
            if(is_string($val)){
                $val = str_replace("$", "", $val);
                $val = str_replace(",", "", $val);
            }
            if(is_numeric($val))
                return floatval($val);
            else
                return 0;
        }
        if ($type === DataType::DateTime 
                || $type == DataType::Date 
                || $type == DataType::Time) {
            if (!$fmt) {
                $fmt = 'd/m/Y';
            } else {
                $fmt = str_replace("-", "/", $fmt);
            }
            return \DateTime::createFromFormat($fmt, str_replace("-", "/", $val));
            }

        return $val;
    }
  /*  
    public static function getDBType($type){
        if ($type == DataType::Int) {
            return "int";
        }
        if ($type == DataType::String) {
            return "varchar";
        }
        if ($type == DataType::Bool) {
            return "int";
        }
        if ($type == DataType::Currency) {
            return "int";
        }
        if ($type == DataType::DateTime || $type == DataType::Date) {
            return "date";
        }
        if ($type == DataType::DateTime || $type == DataType::Date) {
            return "date";
        }
        throw new Exception("Unsupported dateType:$type");
    }
   * 
   */
}
